/**
 * Copyright 2013 Mr.Hai Mobile Developer. All rights reserved.
 * HAI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hector.meinewerk.utils;

import java.util.Timer;
import java.util.TimerTask;

import com.hector.meinewerk.common.GlobalInfo;
import com.hector.meinewerk.listener.TimeoutListener;

/**
 * 
 * Timeout
 * 
 * @author: HaiTC3
 * @version: 1.1
 * @since: 1.0 | Mar 7, 2013
 */
public class Timeout implements TimeoutListener {
	Timer timeOutTimer = null;// quan ly timeout
	// Activity context;//context
	TimeoutListener listener;
	TimeOutTask timeOutTask;

	class TimeOutTask extends TimerTask {
		public boolean isCancel = false;

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.TimerTask#run()
		 */
		@Override
		public void run() {
			// TODO Auto-generated method stub
			GlobalInfo.getInstance().getAppHandler().post(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					if (!isCancel) {
						Timeout.this.stopTimeout();
						listener.onTimeout(Timeout.this);
					}
				}
			});
		}
	}

	public Timeout(TimeoutListener listener) {
		this.listener = listener;
	}

	public void starTimeout(int time) {
		timeOutTimer = new Timer();
		timeOutTask = new TimeOutTask();
		timeOutTimer.schedule(timeOutTask, time);
	}

	public void stopTimeout() {
		// TODO Auto-generated method stub
		if (timeOutTimer != null) {
			timeOutTimer.cancel();
			timeOutTimer = null;

			timeOutTask.isCancel = true;
			timeOutTask.cancel();
			timeOutTask = null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hai.utils.TimeoutListener#onTimeout(com.hai .utils.Timeout)
	 */
	@Override
	public void onTimeout(Timeout timeout) {
		// TODO Auto-generated method stub

	}
}